#include "THnSparse.h"

void mergeScript()
{
	// load libraries
    gSystem->Load("libCore.so");        
    gSystem->Load("libGeom.so");
    gSystem->Load("libVMC.so");
    gSystem->Load("libPhysics.so");
    gSystem->Load("libTree.so");
    gSystem->Load("libSTEERBase.so");
    gSystem->Load("libESD.so");
    gSystem->Load("libAOD.so");
    gSystem->Load("libANALYSIS.so");
    gSystem->Load("libANALYSISalice.so");
	
	gROOT->ProcessLine(".! ls *.root > files.txt");	
	TFileMerger merger;
	FILE *f;
	f = fopen("files.txt", "r");
	if ( f == 0 )
		cout << "no such a file!" << endl;
	char strFileName[120];
	char strFileNameCopy[120];
	while ( f != EOF )
	{
		fscanf(f, "%s", strFileName );
		if ( strstr( strFileNameCopy, strFileName ) )
		{
			cout << "end of file reached! (reading the same line)" << endl;
			break;
		}
		strcpy( strFileNameCopy, strFileName ); //remember name
		cout << strFileName << endl;
		merger.AddFile( strFileName );
	}
	cout << "Start merging..." << endl;
	merger.OutputFile("MergedOutput.root");
	merger.Merge();
	cout << "Merging finished." << endl;
}
